package com.njit.mailm.core.controller;

import com.njit.mailm.commmon.constant.MailConstant;
import com.njit.mailm.core.bean.Draft;
import com.njit.mailm.core.bean.Email;
import com.njit.mailm.core.bean.MessageWrap;
import com.njit.mailm.core.bean.Page;
import com.njit.mailm.core.service.DraftService;
import com.njit.mailm.core.service.MailService;
import com.njit.mailm.core.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2016/4/17.
 */
@RequestMapping("/list")
@Controller
public class ListController {

    @Autowired
    private MailService mailService;

    @Autowired
    private ManagerService managerService;

    @Autowired
    private DraftService draftService;

    @RequestMapping(value = "/listReceive")
    public String listReceive(HttpSession session,Integer page,Model model){
        Email email = (Email) session.getAttribute(MailConstant.CURRENTEMAIL);
        Page p = new Page();
        p.setPage(page);
        p.setTotalItems(mailService.getMailTotal(email));
        List<MessageWrap> messages = mailService.pageList(p,email);
        model.addAttribute("messages", messages);
        model.addAttribute("currentPage", p);
        model.addAttribute("controller","inbox");
        return "mailList";
    }

    @RequestMapping(value = "listStar")
    public String listStar(HttpSession session,Integer page,Model model){
        Email email = (Email) session.getAttribute(MailConstant.CURRENTEMAIL);
        Page p = new Page();
        p.setPage(page);
        p.setTotalItems(managerService.getStarTotal(email));
        List<MessageWrap> messages = managerService.pageListStar(p,email);
        model.addAttribute("messages", messages);
        model.addAttribute("currentPage", p);
        model.addAttribute("controller","inbox");
        return "mailList";
    }

    @RequestMapping(value = "listTrash")
    public String listTrash(HttpSession session,Integer page,Model model){
        Email email = (Email) session.getAttribute(MailConstant.CURRENTEMAIL);
        Page p = new Page();
        p.setPage(page);
        p.setTotalItems(managerService.getTrashTotal(email));
        List<MessageWrap> messages = managerService.pageListTrash(p,email);
        model.addAttribute("messages", messages);
        model.addAttribute("currentPage", p);
        model.addAttribute("controller","trashBox");
        return "mailList";
    }

    @RequestMapping(value = "listDraft")
    public String listDraft(Model model,HttpSession session,Integer page){
        Email email = (Email) session.getAttribute(MailConstant.CURRENTEMAIL);
        Page p = new Page();
        p.setPage(page);
        p.setTotalItems(draftService.getDraftTotal(email));
        List<Draft> draft = draftService.pageListDraft(p,email);
        model.addAttribute("messages", draft);
        model.addAttribute("currentPage", p);
        model.addAttribute("controller","draftBox");
        return "mailList";
    }
}
